define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'classes/teachers/index',
                    add_url: 'classes/teachers/add',
                    edit_url: 'classes/teachers/edit',
                    del_url: 'classes/teachers/del',
                    multi_url: 'classes/teachers/multi',
                    dragsort_url: '',
                    table: 'classes_teachersclasses',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'profile_id',
                sortName: 'profile_id',
                undefinedText: '无',
                columns: [
                    [
                        {checkbox: true},
                        {
                            field: 'classes_id',
                            title: __('Classes'),
                            visible: false,
                            addclass: 'selectpage',
                            extend: 'data-source="classes/classes/index" data-field="title"',
                            operate: '=',
                            formatter: Table.api.formatter.search
                        },
                        {field: 'user_id', title: __('UserId')},
                        {field: 'avatar', title: __('Avatar'), events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'realname', sortable: true, title: __('Realname'),operate: 'LIKE'},
                        {field: 'mobile', title: __('Mobile'),operate: 'LIKE'},
                        {field: 'gender', sortable: true, title: __('Gender'), sortable:true, formatter: function(value, row, index){
                                return value == 0 ? '男' : (value == 1 ? '女' : '未知')
                            }},
                        //{field: 'status', title: __('Status'), searchList: {"1": __('通过'), "0": __('待审批'), "-1": __('不通过')}, formatter: Table.api.formatter.toggle},
                        /*{
                            field: 'buttons',title: __('Teachersclasses'), table: table,
                            buttons: [{
                                name: 'teachersclasses',
                                text: __('View'),
                                classname: 'btn btn-xs btn-success btn-dialog',
                                icon: 'fa fa-tasks',
                                url: 'classes/teachersclasses/index?profile_id={ids}'
                            }],
                            formatter: Table.api.formatter.buttons
                        },*/
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ],
                search: true,
                commonSearch: true
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            /*if(Config.isSuperAdmin){
                schoolChange();
            }else{
                typeChange();
            }
            $("#c-admin_id").change(function(){
                $('#c-classes_id').selectPageClear();
                schoolChange();
            });
            $("#c-classes_id").change(function(){
                $('#c-subject_id').selectPageClear();
                typeChange();
            });*/

            $("#c-classes_id").data("params", function(){
                return {"custom": {"admin_id": $("#c-admin_id").val(),'status':'normal' }};
            });
            Controller.api.bindevent();

        },
        edit: function () {
            if(Config.isSuperAdmin){
                schoolChange();
            }else{
                typeChange();
            }
            Controller.api.bindevent();
            $("#c-admin_id").change(function(){
                $('#c-classes_id').selectPageClear();
                schoolChange();
            });
            $("#c-classes_id").change(function(){
                $('#c-subject_id').selectPageClear();
                typeChange();
            });
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    function schoolChange(){
        $admin_id = $("#c-admin_id").val();
        $("#c-classes_id").data("params", function(){
            return {"custom[admin_id]": $admin_id, "custom[status]": 1};
        });
        typeChange();
    }
    function typeChange(){
        $classes_id = $("#c-classes_id").val();
        $("#c-subject_id").data("params", function(){
            return {"custom[classes_id]": $classes_id, "custom[status]": 1};
        });
    }
    return Controller;
});